package com.huang.lettersort;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by huangRZ on 2016/9/15.
 */
public class QuickIndexBar extends View {

    private String[] indexArr = { "A", "B", "C", "D", "E", "F", "G", "H",
            "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
            "V", "W", "X", "Y", "Z" };
    private int measuredWidth;
    private int measuredHeight;
    private Paint paint;
    private float cellHeight;

    public QuickIndexBar(Context context) {
        super(context);
        init();
    }

    public QuickIndexBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public QuickIndexBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setTextSize(Utils.dip2px(getContext(),16));
        paint.setColor(Color.WHITE);
        paint.setTextAlign(Paint.Align.CENTER);//设置文字底边居中
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /** 初始化控件宽高 */
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        measuredWidth = getMeasuredWidth();
        measuredHeight = getMeasuredHeight();
        /** 计算每个字母的格子高度 */
        cellHeight = (float) measuredHeight / indexArr.length;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        for(int i = 0; i < indexArr.length; i++){
            /** 计算文字高度的一半 */
            float y = i*cellHeight + cellHeight / 2 + getTextHeight(indexArr[i])/2;
            float x = measuredWidth / 2;

            paint.setColor(lastIndex == i ? Color.BLACK : Color.WHITE);

            canvas.drawText(indexArr[i], x, y, paint);
        }
    }

    /** 获取文本的高度 */
    private int getTextHeight(String text) {
        Rect bounds = new Rect();
        paint.getTextBounds(text, 0, text.length(), bounds);
        return bounds.height();
    }


    private int lastIndex = -1;//为避免重复获取同一个index，通过lastIndex判断是否为同一个index
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                float y = event.getY();
                float x = event.getX();
                /** 防止角标越界  做安全性判断 */
                if (y > 0 && y < measuredHeight && x > 0 && x < measuredWidth) {
                    /** 判断触摸的位置 */
                    int index = (int) (y / cellHeight);
                    if (lastIndex != index) {
                        listener.onLetterSelect(indexArr[index]);
                        lastIndex = index;
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                /** -1表示不再获取index */
                lastIndex = -1;
            break;

            default :
            break;
        }
        invalidate();//刷新界面
        return true;
    }

    public void setOnIndexSelectListener(OnIndexSelectListener listener){
        this.listener = listener;
    }

    private OnIndexSelectListener listener;
    public interface OnIndexSelectListener{
        void onLetterSelect(String index);
    }
}
